﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CycleCountRuleBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICycleCountRuleDA cycleCountRuleDA = DataAccess.CreateCycleCountRuleDA();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public CycleCountRuleBLL()
        {
            this.pubParasInfo = null;
        }

        public CycleCountRuleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CycleCountRuleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> ccAdjRuleIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<CycleCountRuleInfo> cycleCountRuleInfos = new List<CycleCountRuleInfo>();
                CycleCountRuleInfo cycleCountRuleInfo = null;
                foreach (string id in ccAdjRuleIDs)
                {
                    cycleCountRuleInfo = cycleCountRuleDA.GetCycleCountRuleByID(dataBase, tran, id, this.dataBaseID[0]);
                    cycleCountRuleInfos.Add(cycleCountRuleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (cycleCountRuleDA.DeleteCycleCountRule(dataBase, tran, ccAdjRuleIDs, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < ccAdjRuleIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<CycleCountRuleInfo>(dataBase, tran, cycleCountRuleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CYCLE_COUNT_RULE", this.dataBaseID);
                        temp = temp + ccAdjRuleIDs[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return cycleCountRuleDA.GetAllCycleCountRule(this.dataBaseID[0]);
        }

        public object GetInfoByID(string CcAdjRuleID)
        {
            return cycleCountRuleDA.GetCycleCountRuleByID(CcAdjRuleID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            List<string> sqlWhere = new List<string>();
            CycleCountRuleQueryEntity cycleCountRuleQuery = obj as CycleCountRuleQueryEntity;
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.CcAdjRuleID))
            {
                sqlWhere.Add(" CC_ADJ_RULE_ID " + cycleCountRuleQuery.CcAdjRuleID);
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + cycleCountRuleQuery.Descr);
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.PosAdjLot))
            {
                sqlWhere.Add(" POS_ADJ_LOT " + cycleCountRuleQuery.PosAdjLot);
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.PosAdjProduceDate))
            {
                sqlWhere.Add(cycleCountRuleQuery.PosAdjProduceDate.Replace("@PosAdjProduceDate@", " POS_ADJ_PRODUCE_DATE "));
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.PosAdjExpiryDate))
            {
                sqlWhere.Add(cycleCountRuleQuery.PosAdjExpiryDate.Replace("@PosAdjExpiryDate@", " POS_ADJ_EXPIRY_DATE "));
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.PosAdjReceiptDate))
            {
                sqlWhere.Add(cycleCountRuleQuery.PosAdjReceiptDate.Replace("@PosAdjReceiptDate@", " POS_ADJ_RECEIPT_DATE "));
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.QtyGtrZero))
            {
                sqlWhere.Add(" QTY_GTR_ZERO " + cycleCountRuleQuery.QtyGtrZero);
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.NegAdjLot))
            {
                sqlWhere.Add(" NEG_ADJ_LOT " + cycleCountRuleQuery.NegAdjLot);
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.NegAdjProduceDate))
            {
                sqlWhere.Add(cycleCountRuleQuery.NegAdjProduceDate.Replace("@NegAdjProduceDate@", " NEG_ADJ_PRODUCE_DATE "));
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.NegAdjExpiryDate))
            {
                sqlWhere.Add(cycleCountRuleQuery.NegAdjExpiryDate.Replace("@NegAdjExpiryDate@", " NEG_ADJ_EXPIRY_DATE "));
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.NegAdjReceiptDate))
            {
                sqlWhere.Add(cycleCountRuleQuery.NegAdjReceiptDate.Replace("@NegAdjReceiptDate@", " NEG_ADJ_RECEIPT_DATE "));
            }
            if (!string.IsNullOrEmpty(cycleCountRuleQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + cycleCountRuleQuery.IsActive);
            }
            return cycleCountRuleDA.GetCycleCountRuleByQueryList(sqlWhere, cycleCountRuleQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strCcAdjRuleID = string.Empty;
            try
            {
                CycleCountRuleInfo cycleCountRule = obj as CycleCountRuleInfo;
                if (cycleCountRuleDA.CheckCycleCountRuleIDUnique(dataBase, tran, cycleCountRule.CcAdjRuleID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2620_004", this.pubParasInfo.UserLanguage, new List<string> { cycleCountRule.CcAdjRuleID });
                }
                cycleCountRule.UpdatedBy = cycleCountRule.CreatedBy;
                cycleCountRule.CreatedDate = DateTime.Now;
                cycleCountRule.UpdatedDate = DateTime.Now;
                if (cycleCountRuleDA.InsertCycleCountRule(dataBase, tran, cycleCountRule, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CycleCountRuleInfo>(dataBase, tran, null, cycleCountRule, AuditAction.Insert, this.pubParasInfo.UserID, "CYCLE_COUNT_RULE", this.dataBaseID);
                    strCcAdjRuleID = cycleCountRule.CcAdjRuleID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strCcAdjRuleID;
        }

        public string Update(object obj)
        {
            string strCcAdjRuleID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                CycleCountRuleInfo cycleCountRule = obj as CycleCountRuleInfo;
                cycleCountRule.CreatedDate = DateTime.Now;
                cycleCountRule.UpdatedDate = DateTime.Now;
                CycleCountRuleInfo oldCycleCountRuleInfo = cycleCountRuleDA.GetCycleCountRuleByID(dataBase, tran, cycleCountRule.CcAdjRuleID, this.dataBaseID[0]);
                if (cycleCountRuleDA.UpdateCycleCountRule(dataBase, tran, cycleCountRule, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CycleCountRuleInfo>(dataBase, tran, oldCycleCountRuleInfo, cycleCountRule, AuditAction.Update, this.pubParasInfo.UserID, "CYCLE_COUNT_RULE", this.dataBaseID);
                    strCcAdjRuleID = cycleCountRule.CcAdjRuleID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strCcAdjRuleID;
        }
    }
}

